

#Clear R
rm(list = ls())

#Set working directory
setwd("/Users/patrickhulme/Dropbox/Replicate/War_and_Responsibility")







###################################Figure 1 in main text###################################

rm(list = ls())

library(readxl)
library(readr)
MIP_Dataset_2022 <- read_excel("Data_files/MIP-Dataset_2022.xlsx")

#Remove cases without names
MIP_Dataset_2022 <- subset(MIP_Dataset_2022, !is.na(MIP_Dataset_2022$Name))

#Remove cases with unknown or n/a U.S. involvement
MIP_Dataset_2022 <- subset(MIP_Dataset_2022, !is.na(MIP_Dataset_2022$BattleLevUS))
MIP_Dataset_2022 <- subset(MIP_Dataset_2022, MIP_Dataset_2022$BattleLevUS != "N/A")
MIP_Dataset_2022 <- subset(MIP_Dataset_2022, MIP_Dataset_2022$BattleLevUS != "Unknown")

MIP_Dataset_2022$BattleFatUS <- as.numeric(MIP_Dataset_2022$BattleFatUS)

MIP_Dataset_2022 <- MIP_Dataset_2022[c(3,5,9:14,43)]

#Drop pre-1897 Cases
MIP_Dataset_2022 <- subset(MIP_Dataset_2022,MIP_Dataset_2022$styear > 1897)

#Remove friendly fire cases (USS Libery and USS Stark incidents)
MIP_Dataset_2022 <- subset(MIP_Dataset_2022, MIP_Dataset_2022$Name != "Six Day War Rescue")
MIP_Dataset_2022 <- subset(MIP_Dataset_2022, MIP_Dataset_2022$Name != "USS Stark Incident")

#Remove cases that subsumed into larger wars/ interventions (duplicates)
##Part of Operation Restore Hope (Somalia)
MIP_Dataset_2022 <- subset(MIP_Dataset_2022, MIP_Dataset_2022$Name != "UNOSOM II")

##Part of War in Afghanistan
MIP_Dataset_2022 <- subset(MIP_Dataset_2022, MIP_Dataset_2022$Name != "Operation Enduring Freedom Phase 1")
MIP_Dataset_2022 <- subset(MIP_Dataset_2022, MIP_Dataset_2022$Name != "Operation Freedom Sentinel")

##Part of U.S. War in Southeast Asia (Vietnam)
MIP_Dataset_2022 <- subset(MIP_Dataset_2022, MIP_Dataset_2022$Name != "Laos Civil War")
MIP_Dataset_2022 <- subset(MIP_Dataset_2022, MIP_Dataset_2022$Name != "Military Advisors to Vietnam")

#Corrections to Dates and American combat fatality statistics

MIP_Dataset_2022$styear[MIP_Dataset_2022$Name == "Bay of Pigs Invasion"] <- 1961 #Corrected year
MIP_Dataset_2022$endday[MIP_Dataset_2022$Ongoing == 1] <- 31 #Add dec 31 2021 end date for ongoing cases
MIP_Dataset_2022$endmon[MIP_Dataset_2022$Ongoing == 1] <- 12 #Add dec 31 2021 end date for ongoing cases
MIP_Dataset_2022$endyear[MIP_Dataset_2022$Ongoing == 1] <- 2021 #Add dec 31 2021 end date for ongoing cases

MIP_Dataset_2022$BattleFatUS[MIP_Dataset_2022$Name == "The Philippine-American War"] <- 1020 # U.S. combat fatalities U.S. combat fatalities  1020  https://en.wikipedia.org/wiki/United_States_military_casualties_of_war
MIP_Dataset_2022$BattleFatUS[MIP_Dataset_2022$Name == "Instability in the Dominican Republic"] <- 17 # U.S. combat fatalities 17 https://www.usmcu.edu/Research/Marine-Corps-History-Division/Research-Tools-Facts-and-Figures/Marine-Corps-Casualties-1775-2016/
MIP_Dataset_2022$BattleFatUS[MIP_Dataset_2022$Name == "Vladivostok Marine deployment"] <- 160 #U.S. combat fatalities 160 https://en.wikipedia.org/wiki/American_Expeditionary_Force%2C_North_Russia ; https://en.wikipedia.org/wiki/United_States_military_casualties_of_war
MIP_Dataset_2022$BattleFatUS[MIP_Dataset_2022$Name == "Removal of Nicaraguan President/civil war"] <- 48 #U.S. combat fatalities 48 https://www.usmcu.edu/Research/Marine-Corps-History-Division/Research-Tools-Facts-and-Figures/Marine-Corps-Casualties-1775-2016/
MIP_Dataset_2022$BattleFatUS[MIP_Dataset_2022$Name == "Bay of Pigs Invasion"] <- 0 #Deaths not U.S. combat fatalities
MIP_Dataset_2022$BattleFatUS[MIP_Dataset_2022$Name == "Operation Power Pack"] <- 27 #U.S. combat fatalities 27 https://www.usmcu.edu/Research/Marine-Corps-History-Division/Research-Tools-Facts-and-Figures/Marine-Corps-Casualties-1775-2016/ https://www.degruyter.com/document/doi/10.1515/9780822376521-084/pdf#:~:text=More%20than%203%2C000%20Dominicans%20died,and%20paratroopers%20killed%20in%20action.
MIP_Dataset_2022$BattleFatUS[MIP_Dataset_2022$Name == "Operation Restore Hope"] <-26 #U.S. combat fatalities 26 https://en.wikipedia.org/wiki/United_Nations_Operation_in_Somalia_II
MIP_Dataset_2022$BattleFatUS[MIP_Dataset_2022$Name == "NATO Intervention in Bosnia"] <- 1 #U.S. combat fatalities 1 https://en.wikipedia.org/wiki/United_States_military_casualties_of_war
MIP_Dataset_2022$BattleFatUS[MIP_Dataset_2022$Name == "Operation Inherent Resolve"] <- 20 #U.S. combat fatalities 20 https://www.defense.gov/casualty.pdf


#Add in data regarding domestic authorization status (AUMF)
MIP_Data_w_Cong_Auth <- read_csv("Data_files/AUMF_Status_MIP.csv")
MIP_Data_w_Cong_Auth <- MIP_Data_w_Cong_Auth[c("styear","Name","Name2","AUMF")]

#Drop pre-1897 Cases
MIP_Data_w_Cong_Auth <- subset(MIP_Data_w_Cong_Auth,MIP_Data_w_Cong_Auth$styear > 1897)

#Drop duplicates
MIP_Data_w_Cong_Auth$is_duplicate <- duplicated(MIP_Data_w_Cong_Auth)
MIP_Data_w_Cong_Auth <- subset(MIP_Data_w_Cong_Auth,MIP_Data_w_Cong_Auth$is_duplicate == 0)

MIP_Data_w_Cong_Auth <- merge(MIP_Dataset_2022,MIP_Data_w_Cong_Auth, by = c("Name","styear"), all.x =T)

#Add authorization status legend
MIP_Data_w_Cong_Auth$DomAuth <- NA
MIP_Data_w_Cong_Auth$DomAuth[MIP_Data_w_Cong_Auth$AUMF== 2] <- "Congressional Authorization"
MIP_Data_w_Cong_Auth$DomAuth[MIP_Data_w_Cong_Auth$AUMF== 1] <- "Congressional Authorization Disputed"
MIP_Data_w_Cong_Auth$DomAuth[MIP_Data_w_Cong_Auth$AUMF== 0] <- "No Authorization"

#Log-scale
MIP_Data_w_Cong_Auth$log_US_cbt_dt <- as.numeric(MIP_Data_w_Cong_Auth$BattleFatUS)
MIP_Data_w_Cong_Auth$log_US_cbt_dt <- ifelse(MIP_Data_w_Cong_Auth$log_US_cbt_dt == 0, 0.1, MIP_Data_w_Cong_Auth$log_US_cbt_dt)
MIP_Data_w_Cong_Auth$log_US_cbt_dt <- log10(MIP_Data_w_Cong_Auth$log_US_cbt_dt)


MIP_Data_w_Cong_Auth$Name2 <- as.character(MIP_Data_w_Cong_Auth$Name2)

library(stringr)
library(ggrepel)

MIP_Data_w_Cong_Auth$start_date <-as.Date(with(MIP_Data_w_Cong_Auth,paste(styear,stmon,stday,sep="-")),"%Y-%m-%d")
MIP_Data_w_Cong_Auth$end_date <-as.Date(with(MIP_Data_w_Cong_Auth,paste(endyear,endmon,endday,sep="-")),"%Y-%m-%d")

MIP_Data_w_Cong_Auth$mid_year <- (MIP_Data_w_Cong_Auth$endyear + MIP_Data_w_Cong_Auth$styear)/2
MIP_Data_w_Cong_Auth$years <- MIP_Data_w_Cong_Auth$endyear - MIP_Data_w_Cong_Auth$styear + 0.001

MIP_Data_w_Cong_Auth <- subset(MIP_Data_w_Cong_Auth, MIP_Data_w_Cong_Auth$styear > 1897)

MIP_Data_w_Cong_Auth$mid_year <- ifelse(MIP_Data_w_Cong_Auth$Name2 == "Mex. Rev.", MIP_Data_w_Cong_Auth$styear, MIP_Data_w_Cong_Auth$mid_year)
MIP_Data_w_Cong_Auth$mid_year <- ifelse(MIP_Data_w_Cong_Auth$Name2 == "Russ. Civ. War", MIP_Data_w_Cong_Auth$endyear, MIP_Data_w_Cong_Auth$mid_year)

#add jitter
MIP_Data_w_Cong_Auth$log_US_cbt_dt <- MIP_Data_w_Cong_Auth$log_US_cbt_dt + runif(10, min = -0.1, max = 0.1)
MIP_Data_w_Cong_Auth$log_US_cbt_dt <- ifelse(MIP_Data_w_Cong_Auth$BattleFatUS == 0 & !is.na(MIP_Data_w_Cong_Auth$Name2),MIP_Data_w_Cong_Auth$log_US_cbt_dt + 0.2,MIP_Data_w_Cong_Auth$log_US_cbt_dt)
MIP_Data_w_Cong_Auth$start_date


#Note that due to jittering of points for data visualization purposes, observations in the figure will move slightly ///
#each time the figure is run (and, consequently, some of the labels for points will not appear due to being covered by other observations)

pdf("Output/Figure_1.pdf", width = 18, height = 12)
ggplot(MIP_Data_w_Cong_Auth, aes(x=styear, xend=endyear+0.3, y=log_US_cbt_dt, yend=log_US_cbt_dt, color= DomAuth)) +
  geom_segment(size = 2, alpha = 0.7)+
  scale_color_manual(values = c("Congressional Authorization" = "darkorange2",
                                "No Authorization"="deepskyblue4", "Congressional Authorization Disputed"="chartreuse4")) + theme_light()  + geom_text(size=8,check_overlap = TRUE, position =  position_dodge(0.5), data=subset(MIP_Data_w_Cong_Auth,  log_US_cbt_dt > -1), aes(mid_year, log_US_cbt_dt, label= Name2, angle=0, vjust = -0.5, family="Times"))+
  theme(text=element_text(size=30, family="Times")) +
  labs(x="Year",y="U.S. Combat Fatalities") + scale_y_continuous(breaks = -1:6, labels=c("0","1","10","100", "1,000", "10,000", "100,000","0"))+ 
  scale_x_continuous(breaks = c(1780,1800,1820,1840,1860,1880,1900,1920,1940,1960,1980,2000,2020)) +
  theme(legend.title = element_blank(), legend.position="right") +
  annotate("rect", xmin = 1945, xmax = 1950, ymin = -Inf, ymax = Inf,
           alpha = .1,fill = "blue")+ theme(legend.position="bottom")
dev.off()











############Speech Data Processing (Figure 2 in main text, and figures in Appendices###################################

#Here, we are creating the CSUMF_CSS ("Congressional Support Score") dataset, as well as the figures in the paper

#Load Data

rm(list = ls())

library(readr)
CSUMF_speeches <- read_csv("Data_files/CSUMF_speeches.csv") #raw speech data


CSUMF_Crises <- read_csv("Data_files/CSUMF_Crises.csv") #crisis data

#First, we create the handcoded scores from the handcoded (i.e., human-labeled) data
handcoded <- subset(CSUMF_speeches,CSUMF_speeches$human_labeled == 1)

DBP <- subset(handcoded,handcoded$crisname == "DIEN BIEN PHU")


#Tweets are also included in the dataset for post-2016 crises, but are not used in the analysis for this paper (including them in the analysis yields very similar results)
handcoded <- subset(handcoded,handcoded$tweet == 0)

#Create Congressional Support Scores based on the speech codings
## Note we weight by speaker; we limit each individual to one opinion

library(doBy)

handcoded$aggregate_support <- handcoded$Advocates_for_Use_of_American_Military_Force + handcoded$Advocates_for_Use_of_American_Ground_Troops + handcoded$Advocates_for_Use_of_American_Air_Assets + handcoded$Advocates_for_Use_of_American_Naval_Assets
handcoded$aggregate_opposition <- handcoded$Advocates_against_Use_of_American_Military_Force + handcoded$Advocates_against_Use_of_American_Ground_Troops + handcoded$Advocates_against_Use_of_American_Air_Assets + handcoded$Advocates_against_Use_of_American_Naval_Assets
collapse2 <- summaryBy(aggregate_support + aggregate_opposition ~ crisname + crisno + MasterID, FUN=sum, data=handcoded)
collapse2$individual_agg_support <- collapse2$aggregate_support.sum/(collapse2$aggregate_support.sum+collapse2$aggregate_opposition.sum)
collapse2 <- subset(collapse2,!is.na(collapse2$individual_agg_support))
collapse2$count <- 1
collapse2b <- summaryBy(individual_agg_support + count ~ crisname + crisno, FUN=sum, data=collapse2)
collapse2b$avg_agg_support <- collapse2b$individual_agg_support.sum/collapse2b$count.sum
SPEAKERS_FULL <- collapse2b[c("crisno","count.sum")]
SPEAKERS_FULL$Full_set_speakers <- SPEAKERS_FULL$count.sum
SPEAKERS_FULL <- SPEAKERS_FULL[c("crisno","Full_set_speakers")]
CSUMF_CSS <- collapse2b
CSUMF_CSS <- CSUMF_CSS[c(1,2,5)]
CSUMF_CSS <- merge(CSUMF_CSS,SPEAKERS_FULL, by = "crisno")

#As noted in the appendix, we recenter scores at 0 (thus, ranging from -0.5 to +0.5, and prorate speech scores with few speakers toward 0 (neutral)
CSUMF_CSS$avg_agg_support <- CSUMF_CSS$avg_agg_support-0.5
CSUMF_CSS$avg_agg_support_5adjust <- ifelse(CSUMF_CSS$Full_set_speakers > 4,CSUMF_CSS$avg_agg_support, CSUMF_CSS$avg_agg_support*CSUMF_CSS$Full_set_speakers*.2)

rm(list = setdiff(ls(), c("CSUMF_speeches","handcoded","CSUMF_CSS","collapse2","CSUMF_Crises")))




#Create Support Scores by Party

library(doBy)
collapse2$party <- collapse2$MasterID

####Extract lawmaker party from MasterID
substrRight <- function(x, n){
  substr(x, nchar(x)-n+1, nchar(x))
}

collapse2$sixth <- substr(substrRight(collapse2$MasterID,6),1,1)
collapse2$seventh <- substr(substrRight(collapse2$MasterID,7),1,1)
collapse2$party <- ifelse(collapse2$sixth == 0,collapse2$seventh,collapse2$sixth)
collapse2$party <- ifelse(collapse2$party == 1, 100,collapse2$party)
collapse2$party <- ifelse(collapse2$party == 2, 200,collapse2$party)
collapse2 <- subset(collapse2,collapse2$party == 100 |collapse2$party == 200)
collapse2b <- summaryBy(individual_agg_support + count ~ crisname + crisno + party, FUN=sum, data=collapse2)
collapse2b$avg_agg_support <- collapse2b$individual_agg_support.sum/collapse2b$count.sum

#######grab repub and dem speak counts
Repub_speakers <- subset(collapse2b, collapse2b$party == 200)
Repub_speakers <- Repub_speakers[c(2,5)] 
colnames(Repub_speakers)[colnames(Repub_speakers) == "count.sum"] <- "Repub_speakers_full"

Dem_speakers <- subset(collapse2b, collapse2b$party == 100)
Dem_speakers <- Dem_speakers[c(2,5)] 
colnames(Dem_speakers)[colnames(Dem_speakers) == "count.sum"] <- "Dem_speakers_full"


CSUMF_CSS_party <- collapse2b
CSUMF_CSS_party <- CSUMF_CSS_party[c(1,2,3,6)]

CSUMF_CSS_Dems <- subset(CSUMF_CSS_party,CSUMF_CSS_party$party == 100)
CSUMF_CSS_Dems$avg_agg_support_Dems <- CSUMF_CSS_Dems$avg_agg_support
CSUMF_CSS_Dems <- CSUMF_CSS_Dems[c(2,5)]

CSUMF_CSS_Repubs <- subset(CSUMF_CSS_party,CSUMF_CSS_party$party == 200)
CSUMF_CSS_Repubs$avg_agg_support_Repubs <- CSUMF_CSS_Repubs$avg_agg_support
CSUMF_CSS_Repubs <- CSUMF_CSS_Repubs[c(2,5)]

CSUMF_CSS <- merge(CSUMF_CSS,CSUMF_CSS_Dems, by = "crisno", all.x = T)
CSUMF_CSS <- merge(CSUMF_CSS,CSUMF_CSS_Repubs, by = "crisno", all.x = T)
CSUMF_CSS <- merge(CSUMF_CSS,Repub_speakers, by = "crisno", all.x = T)
CSUMF_CSS <- merge(CSUMF_CSS,Dem_speakers, by = "crisno", all.x = T)


#As noted in the appendix, we recenter scores at 0 (thus, ranging from -0.5 to +0.5, and prorate speech scores with few speakers toward 0 (neutral)). While we required a minimum of 5 speakers for all of Congress to avoid prorating, for each party we only require 3.
CSUMF_CSS$avg_agg_support_Repubs <- CSUMF_CSS$avg_agg_support_Repubs-0.5
CSUMF_CSS$avg_agg_support_3adjust_Repubs <- ifelse(CSUMF_CSS$Repub_speakers > 2,CSUMF_CSS$avg_agg_support_Repubs, CSUMF_CSS$avg_agg_support_Repubs*CSUMF_CSS$Repub_speakers/3)

CSUMF_CSS$avg_agg_support_Dems <- CSUMF_CSS$avg_agg_support_Dems-0.5
CSUMF_CSS$avg_agg_support_3adjust_Dem <- ifelse(CSUMF_CSS$Dem_speakers > 2,CSUMF_CSS$avg_agg_support_Dems, CSUMF_CSS$avg_agg_support_Dems*CSUMF_CSS$Dem_speakers/3)

library(dplyr)
#Replace NA's with 0
CSUMF_CSS <- CSUMF_CSS %>%
  mutate(across(c("avg_agg_support", "Full_set_speakers","avg_agg_support_5adjust","avg_agg_support_Dems","avg_agg_support_Repubs","Repub_speakers_full","Dem_speakers_full","avg_agg_support_3adjust_Repubs","avg_agg_support_3adjust_Dem"), ~ ifelse(is.na(.), 0, .)))

rm(list = setdiff(ls(), c("CSUMF_speeches","handcoded","CSUMF_CSS","CSUMF_Crises")))




####Figure A7####
Vietnam_war <- subset(CSUMF_CSS, 
                      CSUMF_CSS$crisno == 210|CSUMF_CSS$crisno == 213|CSUMF_CSS$crisno == 225|CSUMF_CSS$crisno == 230|CSUMF_CSS$crisno == 237|CSUMF_CSS$crisno == 241|CSUMF_CSS$crisno == 246|CSUMF_CSS$crisno == 249
                      |CSUMF_CSS$crisno == 10121|CSUMF_CSS$crisno == 10122|CSUMF_CSS$crisno == 10123)

library(readr)
Vietnam_war_months <- read_csv("Data_files/Vietnam_war_crisis_months.csv")

Vietnam_war <- merge(Vietnam_war,Vietnam_war_months, by = c("crisno","crisname"))
Vietnam_war$crisis_month_end_date <- as.Date(Vietnam_war$crisis_month_end_date)

library(ggplot2)
pdf(file = "Output/Figure_A7_Vietnam_Support.pdf",   # The directory you want to save the file in
    width = 11, 
    height = 4)


ggplot(Vietnam_war, aes(crisis_month_end_date, avg_agg_support)) +  geom_line(aes(),width = 0.4, height = 0.2, size = 1, alpha = .6)  + ggtitle("Congressional Support Scores Compared to Congressional Votes") +scale_y_continuous(
  
  # Features of the first axis
  name = "Speech Sentiment", limits=c(-0.50,0.5),
  # Add a second axis and specify its features
  sec.axis = sec_axis( trans=~.+0.5, name="Vote Share",labels = scales::percent_format(scale = 100))
) + xlab("Date") + # for the x axis label
  ylab("Sentiment")+annotate("text", x=as.Date("1964-08-07"), y=(99/(101))-0.575, label= "Gulf of \n Tonkin Res.",family="Times", size=3)+geom_point(aes(x=as.Date('1964-08-07'),y=(99/(101))-0.5),colour="black")+geom_point(aes(x=as.Date('1964-08-07'),y=(422/(435))-0.5),colour="black")+ylab("Sentiment")+geom_point(aes(x=as.Date('1965-05-05'),y=(409/(435))-0.5),colour="black")+geom_point(aes(x=as.Date('1965-05-06'),y=(95/(100))-0.5),colour="black")+annotate("text", x=as.Date('1965-05-06'),y=(95/(100))-0.575, label= "1965 Defense\nSupplemetnal",family="Times", size=3)+geom_point(aes(x=as.Date('1970-06-30'),y=(40/(100))-0.5),colour="black")+geom_point(aes(x=as.Date('1966-03-01'),y=(92/(100))-0.5),colour="black")+annotate("text", x=as.Date('1966-03-01'),y=(92/(100))-0.575, label= "Reject Repeal \n of Tonkin Res.",family="Times", size=3)+geom_point(aes(x=as.Date('1967-03-01'),y=(72/(100))-0.5),colour="black")+annotate("text", x=as.Date('1967-03-01'),y=(372/(435))-0.425, label= "Reject Ban \n on Force against N.V.",family="Times", size=3)+geom_point(aes(x=as.Date('1967-03-02'),y=(372/(435))),colour="black")+annotate("text", x=as.Date('1968-03-01'),y=(372/(435))-0.575, label= "Pledge Support for Pres. Johnson",family="Times", size=3)+geom_point(aes(x=as.Date('1967-03-02'),y=(372/(435))-0.5),colour="black")+geom_point(aes(x=as.Date('1970-07-09'),y=(237/(435))-0.5),colour="black")+annotate("text", x=as.Date("1970-07-09"), y=(237/(435))-0.35, label= "Cooper-Church \n Amendment",family="Times", size=3)+geom_point(aes(x=as.Date('1971-06-22'),y=(42/(57+42))-0.5),colour="black")+annotate("text", x=as.Date('1971-06-22'),y=(42/(57+42))-0.575, label= "Mansfield Amendment\nAdopted",family="Times", size=3)+geom_point(aes(x=as.Date('1973-06-26'),y=(86/(435))-0.5),colour="black")+geom_point(aes(x=as.Date('1973-06-29'),y=(16/(100))-0.5),colour="black")+annotate("text", x=as.Date("1974-01-09"), y=16/(100)-0.5, label= "Case-Church \n Amendment",family="Times", size=3)+
  geom_line(data = Vietnam_war, aes(x = crisis_month_end_date, y = avg_agg_support_Repubs), color = "red") +
  geom_line(data = Vietnam_war, aes(x = crisis_month_end_date, y = avg_agg_support_Dems), color = "blue") + theme_bw()+ geom_hline(yintercept=0, linetype="dashed")+geom_vline(xintercept=as.Date('1969-01-20'))+                        
  scale_x_date(limits = as.Date(c("1964-01-01", "1974-01-30")))+
  theme(text = element_text(family = "Times"))+annotate("rect", xmin = as.Date('1968-01-01'), xmax = as.Date('1968-04-01'), ymin = -Inf, ymax = Inf,
                                                        alpha = .1,fill = "blue") + annotate("text", x=as.Date("1968-02-15",), y=-0.49, label= "Tet Offensive",family="Times", size=4, angle = 90, hjust = 0)+annotate("rect", xmin = as.Date('1964-07-01'), xmax = as.Date('1964-09-01'), ymin = -Inf, ymax = Inf,
                                                                                                                                                                                                                       alpha = .1,fill = "blue") + annotate("text", x=as.Date("1964-08-01",), y=-0.49, label= "Gulf of Tonkin Incident",family="Times", size=4, angle = 90, hjust = 0)+annotate("rect", xmin = as.Date('1965-04-01'), xmax = as.Date('1965-12-01'), ymin = -Inf, ymax = Inf,
                                                                                                                                                                                                                                                                                                                                                                                                alpha = .1,fill = "blue") + annotate("text", x=as.Date("1965-08-01",), y=-0.49, label= "Americanization",family="Times", size=4, angle = 90, hjust = 0)+annotate("rect", xmin = as.Date('1970-04-01'), xmax = as.Date('1970-6-30'), ymin = -Inf, ymax = Inf,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 alpha = .1,fill = "blue") + annotate("text", x=as.Date("1970-05-15",), y=-0.49, label= "Cambodia",family="Times", size=4, angle = 90, hjust = 0)+annotate("rect", xmin = as.Date('1971-01-01'), xmax = as.Date('1971-4-01'), ymin = -Inf, ymax = Inf,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           alpha = .1,fill = "blue") + annotate("text", x=as.Date("1971-02-15",), y=-0.49, label= "Laos",family="Times", size=4, angle = 90, hjust = 0)+annotate("rect", xmin = as.Date('1972-04-01'), xmax = as.Date('1972-8-01'), ymin = -Inf, ymax = Inf,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 alpha = .1,fill = "blue") + annotate("text", x=as.Date("1972-06-01",), y=-0.49, label= "Linebacker",family="Times", size=4, angle = 90, hjust = 0)+annotate("rect", xmin = as.Date('1972-12-01'), xmax = as.Date('1972-12-31'), ymin = -Inf, ymax = Inf,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             alpha = .1,fill = "blue") + annotate("text", x=as.Date("1972-12-15",), y=-0.49, label= "Linebacker II",family="Times", size=4, angle = 90, hjust = 0)+ annotate("text", x=as.Date("1968-12-20",), y=0.49, label= "Johnson",family="Times", size=6, angle = 0, hjust = 1)+ annotate("text", x=as.Date("1969-02-20",), y=0.49, label= "Nixon",family="Times", size=6, angle = 0, hjust = 0)+
  theme(text=element_text(family="Times"))+ annotate("label", x=as.Date("1972-11-01",), y = 0.4, label = "                Congressional Support Score-Overall \n                Cong. Supp. Score-Republicans Only \n             Cong. Supp. Score-Democrats Only", family= "Times", size  = 4) +  geom_segment(aes(x=as.Date("1971-03-15"), y = 0.40, xend =as.Date("1971-10-01"), yend = 0.40), colour = "red", size = 0.5, width = 0.4, alpha = 1)+  geom_segment(aes(x=as.Date("1971-03-15"), y = 0.48, xend =as.Date("1971-10-01"), yend = 0.48),size = 0.75, color = "grey55",  alpha = 0.1)+  geom_segment(aes(x=as.Date("1971-03-15"), y = 0.32, xend =as.Date("1971-10-01"), yend = 0.32),size = 0.5, color = "blue",  alpha = 0.1)

dev.off()





#Figure A6

library(readr)
Vietnam_PubSup <- read_csv("Data_files/Vietnam_PubSup.csv", 
                           col_types = cols(Date = col_date(format = "%m%d%Y")))


Vietnam_PubSup$Date <- as.Date(Vietnam_PubSup$Date, "%m%d%Y") 
Vietnam_PubSup$Percent_Support <- ((Vietnam_PubSup$Oppose/(Vietnam_PubSup$Oppose+Vietnam_PubSup$Support)))*100-50

pdf(file = "Output/Figure_A6_Vietnam_Public_Support.pdf",   # The directory you want to save the file in
    width = 11, 
    height = 4)

ggplot(Vietnam_war, aes(crisis_month_end_date, avg_agg_support)) +  geom_line(aes(),width = 0.4, height = 0.2, size = 1, alpha = .5) + geom_line(data = Vietnam_PubSup, aes(Vietnam_PubSup$Date, Vietnam_PubSup$Percent_Support/100), width = 0.4, height = 0.2, size = 1, alpha = 1, linetype=2) + ggtitle("Congressional Support Scores Compared to Gallup Polls") +scale_y_continuous(
  
  # Features of the first axis
  name = "Speech Sentiment", limits=c(-0.50,0.5),
  # Add a second axis and specify its features
  sec.axis = sec_axis( trans=~.+0.5, name="Public Support",labels = scales::percent_format(scale = 100))
) + xlab("Year") + # for the x axis label
  ylab("Sentiment") + geom_hline(yintercept=0, linetype="dashed", color = "black")+geom_vline(xintercept=as.Date('1969-01-20'))+                        
  scale_x_date(limits = as.Date(c("1964-01-01", "1974-01-30")))+
  theme(text = element_text(family = "Times"))+annotate("rect", xmin = as.Date('1968-01-01'), xmax = as.Date('1968-04-01'), ymin = -Inf, ymax = Inf,
                                                        alpha = .1,fill = "blue") + annotate("text", x=as.Date("1968-02-15",), y=-0.49, label= "Tet Offensive",family="Times", size=4, angle = 90, hjust = 0)+annotate("rect", xmin = as.Date('1964-07-01'), xmax = as.Date('1964-09-01'), ymin = -Inf, ymax = Inf,
                                                                                                                                                                                                                       alpha = .1,fill = "blue") + annotate("text", x=as.Date("1964-08-01",), y=-0.49, label= "Gulf of Tonkin Incident",family="Times", size=4, angle = 90, hjust = 0)+annotate("rect", xmin = as.Date('1965-04-01'), xmax = as.Date('1965-12-01'), ymin = -Inf, ymax = Inf,
                                                                                                                                                                                                                                                                                                                                                                                                alpha = .1,fill = "blue") + annotate("text", x=as.Date("1965-08-01",), y=-0.49, label= "Americanization",family="Times", size=4, angle = 90, hjust = 0)+annotate("rect", xmin = as.Date('1970-04-01'), xmax = as.Date('1970-6-30'), ymin = -Inf, ymax = Inf,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 alpha = .1,fill = "blue") + annotate("text", x=as.Date("1970-05-15",), y=-0.49, label= "Cambodia",family="Times", size=4, angle = 90, hjust = 0)+annotate("rect", xmin = as.Date('1971-01-01'), xmax = as.Date('1971-4-01'), ymin = -Inf, ymax = Inf,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           alpha = .1,fill = "blue") + annotate("text", x=as.Date("1971-02-15",), y=-0.49, label= "Laos",family="Times", size=4, angle = 90, hjust = 0)+annotate("rect", xmin = as.Date('1972-04-01'), xmax = as.Date('1972-8-01'), ymin = -Inf, ymax = Inf,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 alpha = .1,fill = "blue") + annotate("text", x=as.Date("1972-06-01",), y=-0.49, label= "Linebacker",family="Times", size=4, angle = 90, hjust = 0)+annotate("rect", xmin = as.Date('1972-12-01'), xmax = as.Date('1972-12-31'), ymin = -Inf, ymax = Inf,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             alpha = .1,fill = "blue") + annotate("text", x=as.Date("1972-12-15",), y=-0.49, label= "Linebacker II",family="Times", size=4, angle = 90, hjust = 0)+ annotate("text", x=as.Date("1968-12-20",), y=0.49, label= "Johnson",family="Times", size=6, angle = 0, hjust = 1)+ annotate("text", x=as.Date("1969-02-20",), y=0.49, label= "Nixon",family="Times", size=6, angle = 0, hjust = 0)+ theme_bw()+
  theme(text=element_text(family="Times"))+ annotate("label", x=as.Date("1972-12-15",), y = 0.4, label = "                  Congressional Support Score \n             Public Support via Polling", family= "Times", size  = 4) +  geom_segment(aes(x=as.Date("1971-07-30"), y = 0.36, xend =as.Date("1972-04-01"), yend = 0.36), colour = "black", linetype = "dashed", size = 1, alpha = 1)+  geom_segment(aes(x=as.Date("1971-07-30"), y = 0.44, xend =as.Date("1972-04-01"), yend = 0.44),size = 1, color = "grey55",  alpha = 0.1)

dev.off()

rm(list = setdiff(ls(), c("CSUMF_speeches","handcoded","CSUMF_CSS","CSUMF_Crises")))





#Add escalation level from Crises dataset to new dataset (CSUMF_CSS) with Congressional Support Scores
USA_crises <- CSUMF_Crises[c("crisno","crisname","Vietnam War","US_high_act")]

CSUMF_CSS <- merge(USA_crises,CSUMF_CSS, by = c("crisno"), all.x = T)

CSUMF_CSS <- CSUMF_CSS[, !colnames(CSUMF_CSS) %in% "crisname.y"]
colnames(CSUMF_CSS)[colnames(CSUMF_CSS) == "crisname.x"] <- "crisname"

#Make crises which are part of larger, ongoing war
CSUMF_CSS$continuing_war <- 0
CSUMF_CSS$continuing_war <- ifelse(CSUMF_CSS$crisname == "KOREAN WAR II"| CSUMF_CSS$crisname == "KOREAN WAR III"|CSUMF_CSS$crisname == "PLEIKU"|CSUMF_CSS$crisname == "Vietnam Escalation"|CSUMF_CSS$crisname == "Vietnam 67 Key Votes"|CSUMF_CSS$crisname == "TET OFFENSIVE"|CSUMF_CSS$crisname == "VIETNAM SPRING OFF."|CSUMF_CSS$crisname == "INVASION OF CAMBODIA"|CSUMF_CSS$crisname == "INVASION OF LAOS II"|CSUMF_CSS$crisname == "VIETNAM PORTS MINING"|CSUMF_CSS$crisname == "CHRISTMAS BOMBING"
                            |CSUMF_CSS$crisname == "Iraq Surge"|CSUMF_CSS$crisname == "Afghan Surge",1,0)

#Because for some figures (e.g., Figure 9) we are combining all crises that make up a single war into a single crisis observation---using the first crisis of the war---we need to here reclassify the Gulf of Tonkin incident as leading to a full scale war (Vietnam War).
CSUMF_CSS$US_high_act <- ifelse(CSUMF_CSS$crisname == "GULF OF TONKIN",  15, CSUMF_CSS$US_high_act)

#Replace NA's with zeros
CSUMF_CSS <- CSUMF_CSS %>%
  mutate(across(c("avg_agg_support", "Full_set_speakers","avg_agg_support_5adjust","avg_agg_support_Dems","avg_agg_support_Repubs","Repub_speakers_full","Dem_speakers_full","avg_agg_support_3adjust_Repubs","avg_agg_support_3adjust_Dem"), ~ ifelse(is.na(.), 0, .)))

rm(list = setdiff(ls(), c("CSUMF_speeches","handcoded","CSUMF_CSS","CSUMF_Crises")))



##Create additional Cong. Support Scores when limiting speeches to only include those prior to US force initiation
Early_Speeches_Only <- CSUMF_Crises
Early_Speeches_Only <-  Early_Speeches_Only[c(1:10,15)]

DF_Early_Speeches_Only <- merge(handcoded,Early_Speeches_Only, by = "crisno")

DF_Early_Speeches_Only <- subset(DF_Early_Speeches_Only, as.Date(as.character(DF_Early_Speeches_Only$date), "%Y-%m-%d") < as.Date(as.character(DF_Early_Speeches_Only$US_force_init), "%Y%m%d"))


#combined support/ (combined support + combined opposition)
library(doBy)
DF_Early_Speeches_Only$aggregate_support <- DF_Early_Speeches_Only$Advocates_for_Use_of_American_Military_Force + DF_Early_Speeches_Only$Advocates_for_Use_of_American_Ground_Troops + DF_Early_Speeches_Only$Advocates_for_Use_of_American_Air_Assets + DF_Early_Speeches_Only$Advocates_for_Use_of_American_Naval_Assets
DF_Early_Speeches_Only$aggregate_opposition <- DF_Early_Speeches_Only$Advocates_against_Use_of_American_Military_Force + DF_Early_Speeches_Only$Advocates_against_Use_of_American_Ground_Troops + DF_Early_Speeches_Only$Advocates_against_Use_of_American_Air_Assets + DF_Early_Speeches_Only$Advocates_against_Use_of_American_Naval_Assets

collapse2 <- summaryBy(aggregate_support + aggregate_opposition ~ crisname + crisno + MasterID, FUN=sum, data=DF_Early_Speeches_Only)
collapse2$individual_agg_support <- collapse2$aggregate_support.sum/(collapse2$aggregate_support.sum+collapse2$aggregate_opposition.sum)
collapse2 <- subset(collapse2,!is.na(collapse2$individual_agg_support))
collapse2$count <- 1

collapse2b <- summaryBy(individual_agg_support + count ~ crisname + crisno, FUN=sum, data=collapse2)
collapse2b$avg_agg_support <- collapse2b$individual_agg_support.sum/collapse2b$count.sum

SPEAKERS_Pre_Init <- collapse2b[c("crisno","count.sum")]
SPEAKERS_Pre_Init$Pre_init_speakers <- SPEAKERS_Pre_Init$count.sum
SPEAKERS_Pre_Init <- SPEAKERS_Pre_Init[c("crisno","Pre_init_speakers")]

DF_Early_Speeches_Only2 <- collapse2b
DF_Early_Speeches_Only2 <- DF_Early_Speeches_Only2[c(1,4)]

DF_Early_Speeches_Only2$avg_agg_support_Pre_Use_of_Force_ONLY <- DF_Early_Speeches_Only2$avg_agg_support
DF_Early_Speeches_Only2 <- DF_Early_Speeches_Only2[c(1,3)]

DF_Early_Speeches_Only2$avg_agg_support_Pre_Use_of_Force_ONLY <- DF_Early_Speeches_Only2$avg_agg_support_Pre_Use_of_Force_ONLY-0.5
DF_Early_Speeches_Only2 <- merge(DF_Early_Speeches_Only2,SPEAKERS_Pre_Init, by = "crisno")


CSUMF_CSS <- merge(CSUMF_CSS,DF_Early_Speeches_Only2, by = "crisno", all.x = T)
CSUMF_CSS$avg_agg_support_Pre_Use_of_Force_ONLY <- ifelse(is.na(CSUMF_CSS$avg_agg_support_Pre_Use_of_Force_ONLY),CSUMF_CSS$avg_agg_support,CSUMF_CSS$avg_agg_support_Pre_Use_of_Force_ONLY)
CSUMF_CSS$Pre_init_speakers <- ifelse(is.na(CSUMF_CSS$Pre_init_speakers),CSUMF_CSS$Full_set_speakers,CSUMF_CSS$Pre_init_speakers)

rm(list = setdiff(ls(), c("CSUMF_speeches","handcoded","CSUMF_CSS","DF_Early_Speeches_Only","CSUMF_Crises")))


#As noted in appendix, we discount the polarity score for crises in which there were relatively few speakers. I.e., if less than 5 speakers, we discount the polarity toward zero (neutral) depending on the number who spoke
CSUMF_CSS$avg_agg_support_Pre_Use_of_Force_ONLY_5adjust <- ifelse(CSUMF_CSS$Pre_init_speakers > 4,CSUMF_CSS$avg_agg_support_Pre_Use_of_Force_ONLY, CSUMF_CSS$Pre_init_speakers*.2*CSUMF_CSS$avg_agg_support_Pre_Use_of_Force_ONLY)




###Figure 9###

library(ggplot2)

#We are concerned with use of force initiation, so we drop crises which take place during a war (e.g., Tet Offensive during Vietnam War--we simply count all Vietnam War as one observation)

DF2 <- subset(CSUMF_CSS,CSUMF_CSS$continuing_war==0)

#add in nice names
Nice_names <- CSUMF_Crises
Nice_names <- Nice_names[c("crisno","Nice_Name")]

DF2 <- merge(DF2,Nice_names, by = "crisno")

####Mark uses of force that have AUMF
DF2$aumf <- 0
DF2$aumf <- ifelse(DF2$crisname=="CUBAN MISSILES",1,DF2$aumf)
DF2$aumf <- ifelse(DF2$crisname=="GULF OF TONKIN",1,DF2$aumf)
DF2$aumf <- ifelse(DF2$crisname=="Lebanon",1,DF2$aumf)
DF2$aumf <- ifelse(DF2$crisname=="GULF WAR",1,DF2$aumf)
DF2$aumf <- ifelse(DF2$crisname=="IRAQ REGIME CHANGE",1,DF2$aumf)
DF2$aumf <- ifelse(DF2$crisname=="AFGHANISTAN/US",1,DF2$aumf)

#Note the crises we want to be labeled in the figure
Crises <- c("KOREAN WAR I", "CUBAN MISSILES","INVASION OF PANAMA","HAITI MIL. REGIME","YUGOSLAVIA II: BOSNIA (1992-95)","ISIS","GULF WAR","IRAQ REGIME CHANGE","AFGHANISTAN/US","Somalia","GULF OF TONKIN","Lebanon","LIBYAN CIVILWAR","US HOSTAGES IN IRAN","KOSOVO","EC-121 SPY PLANE","Post-Paris Enforcement","El Salvador","SYRIA CHEMICAL WEAPONS","IRAN NUCLEAR II","Nicaragua","Rwandan Genocide","TAIWAN STRAIT I","BERLIN WALL")


USplot <- ggplot(DF2, aes(US_high_act, avg_agg_support_Pre_Use_of_Force_ONLY_5adjust)) + geom_point(alpha = 0) + coord_flip()+
  geom_count(color = "#cccccc")+
  annotate("rect", xmin = 7, xmax = 15, ymin = -0.5, ymax = -0.02,
           alpha = .1,fill = "blue")+ 
  annotate("text", x = 12, y=-0.25, label = "IMPERIAL \n PRESIDENCY", family= "Times")

library(ggrepel)

pdf(file = "Output/Figure_9.pdf",   # The directory you want to save the file in
    width = 9, # The width of the plot in inches
    height = 6)

USplot +   
  geom_point(color = ifelse(DF2$US_high_act < 7,"#cccccc",ifelse(DF2$aumf == 1, "darkorange2","deepskyblue4")))+
  geom_label_repel(aes(family = "Times", label = ifelse(crisname %in% Crises, Nice_Name, ""), size=1.3),
                   box.padding   = 0.85, 
                   point.padding = 0.55,
                   label.size = 0.55,
                   segment.color = 'grey50', min.segment.length = 0,
                   segment.curvature = -1e-40) + geom_vline(xintercept=6.5, linetype="dashed", color = "grey")+
  theme_classic() + coord_flip() + ylab("Congressional Sentiment")+scale_y_continuous(breaks = c(-0.5,-0.25,0,0.25,0.5), labels = c("-0.5 \n Uniform \n Opposition","-0.25","0.0 \n Neutral / Balanced", "0.25","0.5 \n Uniform \n Support"))+ 
  theme(text=element_text(  family="Times"))+ theme(legend.position = "none") + scale_x_discrete(name ="Level of American Escalation \n \n  \ \ \ \ Short of Combat \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \  \ \ \ \  \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ Combat \ \ \ \ \ \ \ \ \ \ \ \n \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \  \ \ \ \  \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (U.S. deaths)", limits=c("No Response","Aid or\n Sanctions","Threats","Show of Force","Military \n Advisers","Uncontested \n Military Action","0","1 to 5","6 to 25","26 to 50","51 to 100","101 to 250","251 to 500","501 to 1000","1000+"))  + theme(axis.text.y = element_text(angle = 0, size = 9)) +
  annotate("text", x=6.75, y=-0.47, label= "Kinetic Military Action", size = 1.6, fontface=3 ,col="grey" ) +
  annotate("text", x=6.25, y=-0.45, label= "Action Short of Armed Conflict", size = 1.6, fontface=3 ,col="grey")

dev.off()






##only include prior to US force initiation REPUBLICAN

DF_Early_Speeches_Only$MasterID2 <- substr( DF_Early_Speeches_Only$MasterID, 1, nchar( DF_Early_Speeches_Only$MasterID)-3)
DF_Early_Speeches_Only$Republican <- grepl('200', DF_Early_Speeches_Only$MasterID2)
DF_Early_Speeches_Only$Democrat <- grepl('100', DF_Early_Speeches_Only$MasterID2)

DF_Early_Speeches_Only_Repub <- subset(DF_Early_Speeches_Only,DF_Early_Speeches_Only$Republican == T)

#combined support/ (combined support + combined opposition)
library(doBy)

DF_Early_Speeches_Only_Repub$aggregate_support <- DF_Early_Speeches_Only_Repub$Advocates_for_Use_of_American_Military_Force + DF_Early_Speeches_Only_Repub$Advocates_for_Use_of_American_Ground_Troops + DF_Early_Speeches_Only_Repub$Advocates_for_Use_of_American_Air_Assets + DF_Early_Speeches_Only_Repub$Advocates_for_Use_of_American_Naval_Assets
DF_Early_Speeches_Only_Repub$aggregate_opposition <- DF_Early_Speeches_Only_Repub$Advocates_against_Use_of_American_Military_Force + DF_Early_Speeches_Only_Repub$Advocates_against_Use_of_American_Ground_Troops + DF_Early_Speeches_Only_Repub$Advocates_against_Use_of_American_Air_Assets + DF_Early_Speeches_Only_Repub$Advocates_against_Use_of_American_Naval_Assets

library(doBy)
collapse2 <- summaryBy(aggregate_support + aggregate_opposition ~ crisname + crisno + MasterID, FUN=sum, data=DF_Early_Speeches_Only_Repub)

collapse2$individual_agg_support <- collapse2$aggregate_support.sum/(collapse2$aggregate_support.sum+collapse2$aggregate_opposition.sum)
collapse2 <- subset(collapse2,!is.na(collapse2$individual_agg_support))
collapse2$count <- 1

collapse2b <- summaryBy(individual_agg_support + count ~ crisname + crisno, FUN=sum, data=collapse2)
collapse2b$avg_agg_support <- collapse2b$individual_agg_support.sum/collapse2b$count.sum

REPUB_SPEAKERS_Pre_Init <- collapse2b[c("crisno","count.sum")]
REPUB_SPEAKERS_Pre_Init$Pre_init_speakers_Repub <- REPUB_SPEAKERS_Pre_Init$count.sum
REPUB_SPEAKERS_Pre_Init <- REPUB_SPEAKERS_Pre_Init[c("crisno","Pre_init_speakers_Repub")]

DF_Early_Speeches_Only_Repub2<- collapse2b
DF_Early_Speeches_Only_Repub2 <- DF_Early_Speeches_Only_Repub2[c(1,4)]
DF_Early_Speeches_Only_Repub2$avg_agg_support_Pre_Use_of_Force_ONLY_REPUB <- DF_Early_Speeches_Only_Repub2$avg_agg_support
DF_Early_Speeches_Only_Repub2 <- DF_Early_Speeches_Only_Repub2[c(1,3)]
DF_Early_Speeches_Only_Repub2$avg_agg_support_Pre_Use_of_Force_ONLY_REPUB <- DF_Early_Speeches_Only_Repub2$avg_agg_support_Pre_Use_of_Force_ONLY_REPUB-0.5
DF_Early_Speeches_Only_Repub2 <- merge(DF_Early_Speeches_Only_Repub2,REPUB_SPEAKERS_Pre_Init, by = "crisno")
CSUMF_CSS <- merge(CSUMF_CSS,DF_Early_Speeches_Only_Repub2, by = "crisno", all.x = T)

CSUMF_CSS$avg_agg_support_Pre_Use_of_Force_ONLY_REPUB <- ifelse(is.na(CSUMF_CSS$avg_agg_support_Pre_Use_of_Force_ONLY_REPUB),CSUMF_CSS$avg_agg_support_Repubs,CSUMF_CSS$avg_agg_support_Pre_Use_of_Force_ONLY_REPUB)

CSUMF_CSS$Pre_init_speakers_Repub <- ifelse(is.na(CSUMF_CSS$Pre_init_speakers_Repub),CSUMF_CSS$Repub_speakers_full,CSUMF_CSS$Pre_init_speakers_Repub)

#As noted in appendix, we discount the polarity score for crises in which there were relatively few speakers. I.e., if less than 3 Republican speakers, we discount the Republican polarity toward zero (neutral) depending on the number who spoke
CSUMF_CSS$avg_agg_support_Pre_Use_of_Force_ONLY_REPUB_3adjust <- ifelse(CSUMF_CSS$Pre_init_speakers_Repub > 2, CSUMF_CSS$avg_agg_support_Pre_Use_of_Force_ONLY_REPUB, (CSUMF_CSS$Pre_init_speakers_Repub*CSUMF_CSS$avg_agg_support_Pre_Use_of_Force_ONLY_REPUB)/3)





##only include prior to US force initiation DEMOCRAT

DF_Early_Speeches_Only_Dem <- subset(DF_Early_Speeches_Only,DF_Early_Speeches_Only$Democrat == T)

#combined support/ (combined support + combined opposition)
library(doBy)


DF_Early_Speeches_Only_Dem$aggregate_support <- DF_Early_Speeches_Only_Dem$Advocates_for_Use_of_American_Military_Force + DF_Early_Speeches_Only_Dem$Advocates_for_Use_of_American_Ground_Troops + DF_Early_Speeches_Only_Dem$Advocates_for_Use_of_American_Air_Assets + DF_Early_Speeches_Only_Dem$Advocates_for_Use_of_American_Naval_Assets
DF_Early_Speeches_Only_Dem$aggregate_opposition <- DF_Early_Speeches_Only_Dem$Advocates_against_Use_of_American_Military_Force + DF_Early_Speeches_Only_Dem$Advocates_against_Use_of_American_Ground_Troops + DF_Early_Speeches_Only_Dem$Advocates_against_Use_of_American_Air_Assets + DF_Early_Speeches_Only_Dem$Advocates_against_Use_of_American_Naval_Assets

library(doBy)
collapse2 <- summaryBy(aggregate_support + aggregate_opposition ~ crisname + crisno + MasterID, FUN=sum, data=DF_Early_Speeches_Only_Dem)
collapse2$individual_agg_support <- collapse2$aggregate_support.sum/(collapse2$aggregate_support.sum+collapse2$aggregate_opposition.sum)
collapse2 <- subset(collapse2,!is.na(collapse2$individual_agg_support))
collapse2$count <- 1

collapse2b <- summaryBy(individual_agg_support + count ~ crisname + crisno, FUN=sum, data=collapse2)
collapse2b$avg_agg_support <- collapse2b$individual_agg_support.sum/collapse2b$count.sum

DEM_SPEAKERS_Pre_Init <- collapse2b[c("crisno","count.sum")]
DEM_SPEAKERS_Pre_Init$Pre_init_speakers_Dem <- DEM_SPEAKERS_Pre_Init$count.sum
DEM_SPEAKERS_Pre_Init <- DEM_SPEAKERS_Pre_Init[c("crisno","Pre_init_speakers_Dem")]

DF_Early_Speeches_Only_Dem2<- collapse2b
DF_Early_Speeches_Only_Dem2 <- DF_Early_Speeches_Only_Dem2[c(1,4)]
DF_Early_Speeches_Only_Dem2$avg_agg_support_Pre_Use_of_Force_ONLY_DEM <- DF_Early_Speeches_Only_Dem2$avg_agg_support
DF_Early_Speeches_Only_Dem2 <- DF_Early_Speeches_Only_Dem2[c(1,3)]
DF_Early_Speeches_Only_Dem2$avg_agg_support_Pre_Use_of_Force_ONLY_DEM <- DF_Early_Speeches_Only_Dem2$avg_agg_support_Pre_Use_of_Force_ONLY_DEM-0.5
DF_Early_Speeches_Only_Dem2 <- merge(DF_Early_Speeches_Only_Dem2,DEM_SPEAKERS_Pre_Init, by = "crisno")

CSUMF_CSS <- merge(CSUMF_CSS,DF_Early_Speeches_Only_Dem2, by = "crisno", all.x = T)

CSUMF_CSS$avg_agg_support_Pre_Use_of_Force_ONLY_DEM <- ifelse(is.na(CSUMF_CSS$avg_agg_support_Pre_Use_of_Force_ONLY_DEM),CSUMF_CSS$avg_agg_support_Dems,CSUMF_CSS$avg_agg_support_Pre_Use_of_Force_ONLY_DEM)

CSUMF_CSS$Pre_init_speakers_Dem <- ifelse(is.na(CSUMF_CSS$Pre_init_speakers_Dem),CSUMF_CSS$Dem_speakers_full,CSUMF_CSS$Pre_init_speakers_Dem)

#As noted in appendix, we discount the polarity score for crises in which there were relatively few speakers. I.e., if less than 3 Democratic speakers, we discount the Democratic polarity toward zero (neutral) depending on the number who spoke
CSUMF_CSS$avg_agg_support_Pre_Use_of_Force_ONLY_DEM_3adjust <- ifelse(CSUMF_CSS$Pre_init_speakers_Dem > 2, CSUMF_CSS$avg_agg_support_Pre_Use_of_Force_ONLY_DEM, (CSUMF_CSS$Pre_init_speakers_Dem*CSUMF_CSS$avg_agg_support_Pre_Use_of_Force_ONLY_DEM)/3)

rm(list = setdiff(ls(), c("CSUMF_speeches","handcoded","CSUMF_CSS","CSUMF_Crises")))





###Figure A5.a

Crises_by_Party <- CSUMF_CSS
library(forcats)
library(dplyr)
library(readxl)
Crisis_names <- CSUMF_Crises
Crisis_names <- Crisis_names[c("crisno","Nice_Name","Show","yrtrig")]
Crisis_names <- subset(Crisis_names,!is.na(Crisis_names$Nice_Name))

Crises_by_Party <- merge(Crises_by_Party,Crisis_names, by = "crisno")
Crises_by_Party <- subset(Crises_by_Party,Crises_by_Party$Pre_init_speakers > 25| Crises_by_Party$Show == 1)

pdf("Output/Figure_A5a_repub_v_dems.pdf", width=5, height=8)
Crises_by_Party %>%
  mutate(Nice_Name = fct_reorder(Nice_Name, desc(avg_agg_support_Pre_Use_of_Force_ONLY_5adjust))) %>%
  ggplot() +
  geom_segment( aes(x=Nice_Name, xend=Nice_Name, y=avg_agg_support_Pre_Use_of_Force_ONLY_REPUB_3adjust, yend=avg_agg_support_Pre_Use_of_Force_ONLY_DEM_3adjust), color="grey") +
  geom_point( aes(x=Nice_Name, y=avg_agg_support_Pre_Use_of_Force_ONLY_DEM_3adjust), color="blue", size=2.4, alpha = 0.6) +
  geom_point( aes(x=Nice_Name, y=avg_agg_support_Pre_Use_of_Force_ONLY_REPUB_3adjust), color="red", size=2.4, alpha = 0.6) +
  geom_point( aes(x=Nice_Name, y=avg_agg_support_Pre_Use_of_Force_ONLY_5adjust), color="black", size=2.4, alpha = 0.6) +
  coord_flip() +
  xlab("") +
  theme_bw()+ geom_hline(yintercept = 0, linetype="dashed", 
                         color = "black", size=0.3)+ 
  theme(text=element_text(  family="Times"))+ ylab("Net Support ")+theme(
    legend.position = "right",
  ) + annotate("label", x = 53, y = 0.28, label = "       Republicans \n   Democrats", family= "Times", size  = 3) + annotate(geom = "point", x = 52.4, y = 0.13, color = "blue", size=2.4, alpha = 0.6 ) + annotate(geom = "point", x =53.6, y =0.13, color = "red",size=2.4, alpha = 0.6 ) 
dev.off()






###Figure A5.b

Prez_party <- CSUMF_Crises
Prez_party <- Prez_party[, c("crisno", "Prez_party")]

Crises_by_Party <- merge(Crises_by_Party,Prez_party, by = "crisno")
Crises_by_Party$avg_agg_support_Pre_Use_of_Force_ONLY_COPARTISANS_3adjust <- ifelse(Crises_by_Party$Prez_party == 100, Crises_by_Party$avg_agg_support_Pre_Use_of_Force_ONLY_DEM_3adjust, Crises_by_Party$avg_agg_support_Pre_Use_of_Force_ONLY_REPUB_3adjust)
Crises_by_Party$avg_agg_support_Pre_Use_of_Force_ONLY_NON_COPARTISANS_3adjust  <- ifelse(Crises_by_Party$Prez_party == 200, Crises_by_Party$avg_agg_support_Pre_Use_of_Force_ONLY_DEM_3adjust, Crises_by_Party$avg_agg_support_Pre_Use_of_Force_ONLY_REPUB_3adjust)

pdf("Output/Figure_A5b_copartisans_v_noncopartisans.pdf", width=5, height=8)
Crises_by_Party %>%
  mutate(Nice_Name = fct_reorder(Nice_Name, desc(avg_agg_support_Pre_Use_of_Force_ONLY_5adjust))) %>%
  ggplot() +
  geom_segment( aes(x=Nice_Name, xend=Nice_Name, y=avg_agg_support_Pre_Use_of_Force_ONLY_COPARTISANS_3adjust, yend=avg_agg_support_Pre_Use_of_Force_ONLY_NON_COPARTISANS_3adjust), color="grey") +
  geom_point( aes(x=Nice_Name, y=avg_agg_support_Pre_Use_of_Force_ONLY_NON_COPARTISANS_3adjust), color="purple", size=2.4, alpha = 0.6) +
  geom_point( aes(x=Nice_Name, y=avg_agg_support_Pre_Use_of_Force_ONLY_COPARTISANS_3adjust), color="grey", size=2.4, alpha = 0.6) +
  geom_point( aes(x=Nice_Name, y=avg_agg_support_Pre_Use_of_Force_ONLY_5adjust), color="black", size=2.4, alpha = 0.6) +
  coord_flip() +
  xlab("") +
  theme_bw()+ geom_hline(yintercept = 0, linetype="dashed", 
                         color = "black", size=0.3)+ 
  theme(text=element_text(  family="Times"))+ ylab("Net Support ")+theme(
    legend.position = "right",
  ) + annotate("label", x = 53, y = 0.28, label = " Copartisans \n      Noncopartisans", family= "Times", size  = 3) + annotate(geom = "point", x = 52.4, y = 0.13, color = "purple", size=2.4, alpha = 0.6 ) + annotate(geom = "point", x =53.6, y =0.13, color = "grey",size=2.4, alpha = 0.6 ) 
dev.off()

rm(list = setdiff(ls(), c("CSUMF_speeches","CSUMF_CSS","CSUMF_Crises")))







###Now we move from the hand-labeled speeches to those with predicted labels from a LLM. This uses GPT-3.5 turbo predicted labels (run March 2023).

CSUMF_speeches$GPT_Irrelevant <- 0
CSUMF_speeches$GPT_Support <- 0
CSUMF_speeches$GPT_Oppose <- 0

CSUMF_speeches$GPT_Support <- ifelse(CSUMF_speeches$GPT == "1-yes",1,0)
CSUMF_speeches$GPT_Oppose <- ifelse(CSUMF_speeches$GPT == "2-no",1,0)
CSUMF_speeches$GPT_Irrelevant <- ifelse(CSUMF_speeches$GPT == "3-unclear",1,0)

#Only include speeches which express sentiment (i.e., support or opposition to use of force)
CSUMF_speeches <- subset(CSUMF_speeches,CSUMF_speeches$GPT_Support == 1 | CSUMF_speeches$GPT_Oppose == 1)

library(doBy)
collapse1 <- summaryBy(GPT_Support + GPT_Oppose ~  MasterID + crisname + crisno, FUN=sum, data=CSUMF_speeches)
collapse1$gpt_support <- collapse1$GPT_Support.sum/(collapse1$GPT_Support.sum+collapse1$GPT_Oppose.sum)

GPT_support_overall <- summaryBy(gpt_support ~ crisname + crisno, FUN = mean, data=collapse1)

collapse1$speaker_count <- 1
Speaker_count_gpt <- summaryBy(speaker_count ~ crisname + crisno, FUN = sum, data=collapse1)

Speaker_count_gpt$GPT_speakers_all <- Speaker_count_gpt$speaker_count.sum
Speaker_count_gpt <- Speaker_count_gpt[c("crisno","GPT_speakers_all")] 

GPT_support_overall$gpt_support <- GPT_support_overall$gpt_support.mean
GPT_support_overall <- GPT_support_overall[c("crisno","crisname","gpt_support")]
GPT_support_overall <- merge(GPT_support_overall,Speaker_count_gpt, by = "crisno")

#Recenter at zero (i.e., Congressional Support Score Ranges from -0.5 to +0.5, instead of 0 to 1)
GPT_support_overall$gpt_support <- GPT_support_overall$gpt_support -0.5

#Prorate score for crises that have fewer than 5 speakers
GPT_support_overall$gpt_support_5_adj <-  ifelse(GPT_support_overall$GPT_speakers_all > 4,GPT_support_overall$gpt_support,GPT_support_overall$gpt_support*GPT_support_overall$GPT_speakers_all/5)




#Limit GPT estimates to pre-use-of force

library(readxl)
Early_Speeches_Only <- CSUMF_Crises
Early_Speeches_Only <-  Early_Speeches_Only[c(1:10)]

DF_Early_Speeches_Only <- merge(CSUMF_speeches,Early_Speeches_Only, by = "crisno")
DF_Early_Speeches_Only <- subset(DF_Early_Speeches_Only, as.Date(as.character(DF_Early_Speeches_Only$date), "%Y-%m-%d") < as.Date(as.character(DF_Early_Speeches_Only$US_force_init), "%Y%m%d"))

library(doBy)
collapse1 <- summaryBy(GPT_Support + GPT_Oppose ~ crisno + crisname + MasterID, FUN=sum, data=DF_Early_Speeches_Only)
collapse1$individual_support <- collapse1$GPT_Support.sum/(collapse1$GPT_Support.sum+collapse1$GPT_Oppose.sum)
collapse1$count <- 1

collapse1b <- summaryBy(individual_support + count ~ crisname + crisno, FUN=sum, data=collapse1)
collapse1b$gpt_support_Pre_Use_of_Force_ONLY <- collapse1b$individual_support.sum/collapse1b$count.sum
collapse1b$GPT_Pre_Use_of_Force_Speakers <- collapse1b$count.sum

GPT_Pre_Use_of_Force <- collapse1b[c("crisno","GPT_Pre_Use_of_Force_Speakers","gpt_support_Pre_Use_of_Force_ONLY")]

#Recenter at zero (i.e., Congressional Support Score Ranges from -0.5 to +0.5, instead of 0 to 1)
GPT_Pre_Use_of_Force$gpt_support_Pre_Use_of_Force_ONLY <- GPT_Pre_Use_of_Force$gpt_support_Pre_Use_of_Force_ONLY -0.5

#Prorate score for crises that have fewer than 5 speakers
GPT_Pre_Use_of_Force$gpt_support_Pre_Use_of_Force_ONLY_5_adj <-  ifelse(GPT_Pre_Use_of_Force$GPT_Pre_Use_of_Force_Speakers > 4,GPT_Pre_Use_of_Force$gpt_support_Pre_Use_of_Force_ONLY,(GPT_Pre_Use_of_Force$gpt_support_Pre_Use_of_Force_ONLY*GPT_Pre_Use_of_Force$GPT_Pre_Use_of_Force_Speakers/5))


############

GPT_support_overall <- merge(GPT_support_overall,GPT_Pre_Use_of_Force, by = "crisno",all.x = T)
GPT_support_overall$GPT_Pre_Use_of_Force_Speakers <- ifelse(is.na(GPT_support_overall$GPT_Pre_Use_of_Force_Speakers),GPT_support_overall$GPT_speakers_all ,GPT_support_overall$GPT_Pre_Use_of_Force_Speakers)
GPT_support_overall$gpt_support_Pre_Use_of_Force_ONLY <- ifelse(is.na(GPT_support_overall$gpt_support_Pre_Use_of_Force_ONLY),GPT_support_overall$gpt_support ,GPT_support_overall$gpt_support_Pre_Use_of_Force_ONLY)
GPT_support_overall$gpt_support_Pre_Use_of_Force_ONLY_5_adj <- ifelse(is.na(GPT_support_overall$gpt_support_Pre_Use_of_Force_ONLY_5_adj),GPT_support_overall$gpt_support_5_adj ,GPT_support_overall$gpt_support_Pre_Use_of_Force_ONLY_5_adj)
GPT_support_overall <- GPT_support_overall[, !colnames(GPT_support_overall) %in% "crisname"]

CSUMF_CSS <- merge(CSUMF_CSS,GPT_support_overall, by = "crisno", all.x = T)

#Replace NA's with 0
CSUMF_CSS <- CSUMF_CSS %>%
  mutate(across(c("gpt_support", "GPT_speakers_all","gpt_support_5_adj","GPT_Pre_Use_of_Force_Speakers","gpt_support_Pre_Use_of_Force_ONLY","gpt_support_Pre_Use_of_Force_ONLY_5_adj"), ~ ifelse(is.na(.), 0, .)))

rm(list = setdiff(ls(), c("CSUMF_speeches","CSUMF_CSS","CSUMF_Crises")))



#####FIGURE 8A

library(readr)
vote_data <- read_csv("Data_files/vote_data.csv")

library(readxl)

DF_Votes <- CSUMF_CSS
DF_Votes <- DF_Votes[c("crisno","avg_agg_support_Pre_Use_of_Force_ONLY_5adjust","gpt_support_Pre_Use_of_Force_ONLY_5_adj","avg_agg_support_5adjust","gpt_support_5_adj")]

vote_data <- merge(vote_data,DF_Votes, by = "crisno", all.x = T)

#recenter range of CSS from "-0.5 to +0.5" to "0 to 1", in order to more easily compare to vote share (which ranges from 0% to 100%)
vote_data$avg_agg_support_5adjust <- vote_data$avg_agg_support_5adjust + 0.5
vote_data$gpt_support_5_adj <- vote_data$gpt_support_5_adj+0.5

#Create DF to store r-squared
Proxies <- c('Hand-Labeled','GPT','Dictionary',"FP Ideal Points","Pres. Support Score","NOMINATE Dim 1","NOMINATE Dim 2","% Republicans","% Copartisans")
performance <- data.frame(Proxies)
performance$R2 <- NA

m1 <- lm(vote_data$avg_agg_support_5adjust ~ vote_data$percent_favor_force)
performance$R2[1] <- summary(m1)$r.squared

m2 <- lm(vote_data$gpt_support_5_adj ~ vote_data$percent_favor_force)
performance$R2[2] <- summary(m2)$r.squared

m3 <- lm(vote_data$ave_sentiment.mean ~ vote_data$percent_favor_force)
performance$R2[3] <- summary(m3)$r.squared

m4 <- lm(vote_data$FP_mean ~ vote_data$percent_favor_force)
performance$R2[4] <- summary(m4)$r.squared

m5 <- lm(vote_data$score ~ vote_data$percent_favor_force)
performance$R2[5] <- summary(m5)$r.squared

m6 <- lm(vote_data$chamber.mean.d1 ~ vote_data$percent_favor_force)
performance$R2[6] <- summary(m6)$r.squared

m7 <- lm(vote_data$chamber.mean.d2 ~ vote_data$percent_favor_force)
performance$R2[7] <- summary(m7)$r.squared

m8 <- lm(vote_data$percent_repub ~ vote_data$percent_favor_force)
summary(m6)
performance$R2[8] <- summary(m8)$r.squared

m9 <- lm(vote_data$prez_copartisans ~ vote_data$percent_favor_force)
performance$R2[9] <- summary(m9)$r.squared


###Create Figure A8

pdf(file = "Output/Figure_A8_proxies_vs_votes.pdf",   # The directory you want to save the file in
    width = 10, # The width of the plot in inches
    height = 10)
par(mfrow = c(3, 3))

plot(vote_data$percent_favor_force,vote_data$avg_agg_support_5adjust, pch = 16,  col = alpha("black", 0.5),main="Congressional Support Scores \n (Hand-Labeled Only)", sub = paste( "R-Squared = ", round(performance$R2[1], digits = 2), sep = ""),xlab="% Vote Favoring Force", ylab="Support In Congressional Speeches",cex.lab = 1.6,cex.main = 1.6,cex.sub=1.5,
     xlim=c(0, 1), ylim=c(0, 1), family = "Times")+ abline(lm(vote_data$avg_agg_support_5adjust ~ vote_data$percent_favor_force)) + segments(0, 0, x1 = 1, y1 = 1,
                                                                                                                                     col = "blue", lty = 2, lwd = par("lwd"))
plot(vote_data$percent_favor_force,vote_data$gpt_support_5_adj, pch = 16,  col = alpha("black", 0.5),main="Congressional Support Scores \n (GTP 3.5)", sub = paste( "R-Squared = ", round(performance$R2[2], digits = 2), sep = ""),
     xlab="% Vote Favoring Force", ylab="Predicted Support In Congr. Speeches",cex.lab = 1.6,cex.main = 1.6,cex.sub=1.5,
     xlim=c(0, 1), ylim=c(0, 1), family = "Times")+ abline(lm(vote_data$gpt_support_5_adj ~ vote_data$percent_favor_force))+ segments(0, 0, x1 = 1, y1 = 1,
                                                                                                                                col = "blue", lty = 2, lwd = par("lwd"))
plot(vote_data$percent_favor_force,vote_data$ave_sentiment.mean,pch = 16,  col = alpha("black", 0.5), main="Dictionary Sentiment", xlab="% Vote Favoring Force", ylab="Dictionary Sentiment",cex.lab = 1.6,cex.main = 1.6,cex.sub=1.5, xlim=c(0, 1), ylim=c(min(vote_data$ave_sentiment.mean), max(vote_data$ave_sentiment.mean)), sub = paste( "R-Squared = ", round(performance$R2[3], digits = 2), sep = ""), family = "Times")+  abline(lm(vote_data$ave_sentiment.mean ~ vote_data$percent_favor_force))+ segments(0,min(vote_data$ave_sentiment.mean), 1, max(vote_data$ave_sentiment.mean),col = "blue", lty = 2, lwd = par("lwd"))

plot(vote_data$percent_favor_force,vote_data$FP_mean,pch = 16,  col = alpha("black", 0.5), main="Jeong FP Ideology Scores", sub = paste( "R-Squared = ", round(performance$R2[4], digits = 2), sep = ""),
     xlab="% Vote Favoring Force", ylab="Jeong FP Ideology Scores",cex.lab = 1.6,cex.main = 1.6,cex.sub=1.5,
     xlim=c(0, 1), ylim=c(min(vote_data$FP_mean,na.rm=TRUE), max(vote_data$FP_mean,na.rm=TRUE)), family = "Times")+ abline(lm(vote_data$FP_mean ~ vote_data$percent_favor_force))+ segments(0,min(vote_data$FP_mean,na.rm=TRUE), 1, max(vote_data$FP_mean,na.rm=TRUE),col = "blue", lty = 2, lwd = par("lwd"))

plot(vote_data$percent_favor_force,vote_data$score, pch = 16,  col = alpha("black", 0.5),main="Presidential Support Scores", xlab="% Vote Favoring Force", ylab="Presidential Support Scores",cex.lab = 1.6,cex.main = 1.6,cex.sub=1.5, sub = paste( "R-Squared = ", round(performance$R2[5], digits = 2), sep = ""),
     xlim=c(0, 1), ylim=c(min(vote_data$score,na.rm=TRUE), max(vote_data$score,na.rm=TRUE)), family = "Times")+  abline(lm(vote_data$score ~ vote_data$percent_favor_force))+ segments(0,min(vote_data$score,na.rm=TRUE), 1, max(vote_data$score,na.rm=TRUE),col = "blue", lty = 2, lwd = par("lwd"))

plot(vote_data$percent_favor_force,vote_data$chamber.mean.d1, pch = 16,  col = alpha("black", 0.5),main="NOMINATE Dimension 1", xlab="% Vote Favoring Force", ylab="NOMINATE Dimension 1",cex.lab = 1.6,cex.main = 1.6,cex.sub=1.5, sub = paste( "R-Squared = ", round(performance$R2[6], digits = 2), sep = ""),
     xlim=c(0, 1), ylim=c(min(vote_data$chamber.mean.d1,na.rm=TRUE), max(vote_data$chamber.mean.d1,na.rm=TRUE)), family = "Times")+  abline(lm(vote_data$chamber.mean.d1 ~ vote_data$percent_favor_force))+ segments(0,min(vote_data$chamber.mean.d1), 1, max(vote_data$chamber.mean.d1),col = "blue", lty = 2, lwd = par("lwd"))

plot(vote_data$percent_favor_force,vote_data$chamber.mean.d2, pch = 16,  col = alpha("black", 0.5),main="NOMINATE Dimension 2", xlab="% Vote Favoring Force", ylab="NOMINATE Dimension 2",cex.lab = 1.6,cex.main = 1.6,cex.sub=1.5,sub = paste( "R-Squared = ", round(performance$R2[7], digits = 2), sep = ""),
     xlim=c(0, 1), ylim=c(min(vote_data$chamber.mean.d2,na.rm=TRUE), max(vote_data$chamber.mean.d2,na.rm=TRUE)), family = "Times")+  abline(lm(vote_data$chamber.mean.d2 ~ vote_data$percent_favor_force))+ segments(0,min(vote_data$chamber.mean.d2), 1, max(vote_data$chamber.mean.d2),col = "blue", lty = 2, lwd = par("lwd"))

plot(vote_data$percent_favor_force,vote_data$percent_repub, pch = 16,  col = alpha("black", 0.5),main="% Congressional Republicans", xlab="% Vote Favoring Force", ylab="% Republicans",cex.lab = 1.6,cex.main = 1.6,cex.sub=1.5, sub = paste( "R-Squared = ", round(performance$R2[8], digits = 2), sep = ""), xlim=c(0, 1), ylim=c(min(vote_data$percent_repub,na.rm=TRUE), max(vote_data$percent_repub,na.rm=TRUE)), family = "Times")+  abline(lm(vote_data$percent_repub ~ vote_data$percent_favor_force))+ segments(0,min(vote_data$percent_repub), 1, max(vote_data$percent_repub),col = "blue", lty = 2, lwd = par("lwd"))

plot(vote_data$percent_favor_force,vote_data$prez_copartisans,pch = 16,  col = alpha("black", 0.5), main="% Congressional Copartisans", xlab="% Vote Favoring Force", ylab="% Congressional Copartisans",cex.lab = 1.6,cex.main = 1.6,cex.sub=1.5,sub = paste( "R-Squared = ", round(performance$R2[9], digits = 2), sep = ""), xlim=c(0, 1), ylim=c(min(vote_data$prez_copartisans,na.rm=TRUE), max(vote_data$prez_copartisans,na.rm=TRUE)), family = "Times")+  abline(lm(vote_data$prez_copartisans ~ vote_data$percent_favor_force))+ segments(0,min(vote_data$prez_copartisans), 1, max(vote_data$prez_copartisans),col = "blue", lty = 2, lwd = par("lwd"))

plot(vote_data$percent_favor_force,vote_data$prez_copartisans, pch = 16,  col = alpha("black", 0.5),main="% Congressional Copartisans", xlab="% Vote Favoring Force", ylab="% Congressional Copartisans",cex.lab = 1.6,cex.main = 1.6,cex.sub=1.5,sub = paste( "R-Squared = ", round(performance$R2[9], digits = 2), sep = ""), xlim=c(0, 1), ylim=c(min(vote_data$prez_copartisans,na.rm=TRUE), max(vote_data$prez_copartisans,na.rm=TRUE)), family = "Times")+  abline(lm(vote_data$prez_copartisans ~ vote_data$percent_favor_force))+ segments(0,min(vote_data$prez_copartisans), 1, max(vote_data$prez_copartisans),col = "blue", lty = 2, lwd = par("lwd"))

dev.off()

rm(list = setdiff(ls(), c("CSUMF_speeches","CSUMF_CSS","CSUMF_Crises")))





#FIGURE A9

library(readr)
Polls <- read_csv("Data_files/polls_data.csv")

DF_Polls <- CSUMF_CSS
DF_Polls <- DF_Polls[c("crisno","avg_agg_support_Pre_Use_of_Force_ONLY_5adjust","gpt_support_Pre_Use_of_Force_ONLY_5_adj","avg_agg_support_5adjust","gpt_support_5_adj")]

Polls <- merge(Polls,DF_Polls, by = "crisno", all.x = T)

Polls$avg_agg_support_5adjust <- Polls$avg_agg_support_5adjust + 0.5
Polls$gpt_support_5_adj <- Polls$gpt_support_5_adj+0.5

#Create DF to store r-squared
Proxies <- c('Hand-Labeled','GPT','Dictionary',"FP Ideal Points","Pres. Support Score","NOMINATE Dim 1","NOMINATE Dim 2","% Republicans","% Copartisans")
performance <- data.frame(Proxies)
performance$R2 <- NA

performance <- data.frame(Proxies)
performance$R2 <- NA

m1 <- lm(Polls$avg_agg_support_5adjust ~ Polls$Pub_supp)
performance$R2[1] <- summary(m1)$r.squared

m2 <- lm(Polls$gpt_support_5_adj ~ Polls$Pub_supp)
performance$R2[2] <- summary(m2)$r.squared

m3 <- lm(Polls$ave_sentiment.mean ~ Polls$Pub_supp)
performance$R2[3] <- summary(m3)$r.squared

m4 <- lm(Polls$FP_mean ~ Polls$Pub_supp)
performance$R2[4] <- summary(m4)$r.squared

m5 <- lm(Polls$score ~ Polls$Pub_supp)
performance$R2[5] <- summary(m5)$r.squared

m6 <- lm(Polls$chamber.mean.d1 ~ Polls$Pub_supp)
performance$R2[6] <- summary(m6)$r.squared

m7 <- lm(Polls$chamber.mean.d2 ~ Polls$Pub_supp)
performance$R2[7] <- summary(m7)$r.squared

m8 <- lm(Polls$percent_repub ~ Polls$Pub_supp)
performance$R2[8] <- summary(m8)$r.squared

m9 <- lm(Polls$prez_copartisans ~ Polls$Pub_supp)
performance$R2[9] <- summary(m9)$r.squared




pdf(file = "Output/Figure_A9_proxies_vs_polls.pdf",   # The directory you want to save the file in
    width = 10, # The width of the plot in inches
    height = 10)
par(mfrow = c(3, 3))

plot(Polls$Pub_supp,Polls$avg_agg_support_5adjust,pch = 16,  col = alpha("black", 0.5), main="Congressional Support Scores \n (Hand-Labeled Only)", sub = paste( "R-Squared = ", round(performance$R2[1], digits = 2), sep = ""),xlab="% Public Favoring Force", ylab="Support In Congressional Speeches",cex.lab = 1.6,cex.main = 1.6,cex.sub=1.5,
     xlim=c(0, 1), ylim=c(0, 1), family = "Times")+ abline(lm(Polls$avg_agg_support_5adjust ~ Polls$Pub_supp)) + segments(0, 0, x1 = 1, y1 = 1,
                                                                                                                  col = "blue", lty = 2, lwd = par("lwd"))

plot(Polls$Pub_supp,Polls$gpt_support_5_adj,pch = 16,  col = alpha("black", 0.5), main="Congressional Support Scores \n (GPT 3.5)", sub = paste( "R-Squared = ", round(performance$R2[2], digits = 2), sep = ""),
     xlab="% Public Favoring Force", ylab="Predicted Support In Congr. Speeches",cex.lab = 1.6,cex.main = 1.6,cex.sub=1.5,
     xlim=c(0, 1), ylim=c(0, 1), family = "Times")+ abline(lm(Polls$gpt_support_5_adj ~ Polls$Pub_supp))+ segments(0, 0, x1 = 1, y1 = 1,
                                                                                                             col = "blue", lty = 2, lwd = par("lwd"))
plot(Polls$Pub_supp,Polls$ave_sentiment.mean, pch = 16,  col = alpha("black", 0.5),main="Dictionary Sentiment", xlab="% Public Favoring Force", ylab="Dictionary Sentiment",cex.lab = 1.6,cex.main = 1.6,cex.sub=1.5, xlim=c(0, 1), ylim=c(min(Polls$ave_sentiment.mean), max(Polls$ave_sentiment.mean)), sub = paste( "R-Squared = ", round(performance$R2[3], digits = 2), sep = ""), family = "Times")+  abline(lm(Polls$ave_sentiment.mean ~ Polls$Pub_supp))+ segments(0,min(Polls$ave_sentiment.mean), 1, max(Polls$ave_sentiment.mean),col = "blue", lty = 2, lwd = par("lwd"))

plot(Polls$Pub_supp,Polls$FP_mean,pch = 16,  col = alpha("black", 0.5), main="Jeong FP Ideology Scores", sub = paste( "R-Squared = ", round(performance$R2[4], digits = 2), sep = ""),
     xlab="% Public Favoring Force", ylab="Jeong FP Ideology Scores",cex.lab = 1.6,cex.main = 1.6,cex.sub=1.5,
     xlim=c(0, 1), ylim=c(min(Polls$FP_mean,na.rm=TRUE), max(Polls$FP_mean,na.rm=TRUE)), family = "Times")+ abline(lm(Polls$FP_mean ~ Polls$Pub_supp))+ segments(0,min(Polls$FP_mean,na.rm=TRUE), 1, max(Polls$FP_mean,na.rm=TRUE),col = "blue", lty = 2, lwd = par("lwd"))

plot(Polls$Pub_supp,Polls$score, pch = 16,  col = alpha("black", 0.5),main="Presidential Support Scores", xlab="% Public Favoring Force", ylab="Presidential Support Scores",cex.lab = 1.6,cex.main = 1.6,cex.sub=1.5, sub = paste( "R-Squared = ", round(performance$R2[5], digits = 2), sep = ""),
     xlim=c(0, 1), ylim=c(min(Polls$score,na.rm=TRUE), max(Polls$score,na.rm=TRUE)), family = "Times")+  abline(lm(Polls$score ~ Polls$Pub_supp))+ segments(0,min(Polls$score,na.rm=TRUE), 1, max(Polls$score,na.rm=TRUE),col = "blue", lty = 2, lwd = par("lwd"))

plot(Polls$Pub_supp,Polls$chamber.mean.d1,pch = 16,  col = alpha("black", 0.5), main="NOMINATE Dimension 1", xlab="% Public Favoring Force", ylab="NOMINATE Dimension 1",cex.lab = 1.6,cex.main = 1.6,cex.sub=1.5, sub = paste( "R-Squared = ", round(performance$R2[6], digits = 2), sep = ""),
     xlim=c(0, 1), ylim=c(min(Polls$chamber.mean.d1,na.rm=TRUE), max(Polls$chamber.mean.d1,na.rm=TRUE)), family = "Times")+  abline(lm(Polls$chamber.mean.d1 ~ Polls$Pub_supp))+ segments(0,min(Polls$chamber.mean.d1), 1, max(Polls$chamber.mean.d1),col = "blue", lty = 2, lwd = par("lwd"))

plot(Polls$Pub_supp,Polls$chamber.mean.d2,pch = 16,  col = alpha("black", 0.5), main="NOMINATE Dimension 2", xlab="% Public Favoring Force", ylab="NOMINATE Dimension 2",cex.lab = 1.6,cex.main = 1.6,cex.sub=1.5,sub = paste( "R-Squared = ", round(performance$R2[7], digits = 2), sep = ""),
     xlim=c(0, 1), ylim=c(min(Polls$chamber.mean.d2,na.rm=TRUE), max(Polls$chamber.mean.d2,na.rm=TRUE)), family = "Times")+  abline(lm(Polls$chamber.mean.d2 ~ Polls$Pub_supp))+ segments(0,min(Polls$chamber.mean.d2), 1, max(Polls$chamber.mean.d2),col = "blue", lty = 2, lwd = par("lwd"))

plot(Polls$Pub_supp,Polls$percent_repub,pch = 16,  col = alpha("black", 0.5), main="% Congressional Republicans", xlab="% Public Favoring Force", ylab="% Republicans",cex.lab = 1.6,cex.main = 1.6,cex.sub=1.5, sub = paste( "R-Squared = ", round(performance$R2[8], digits = 2), sep = ""), xlim=c(0, 1), ylim=c(min(Polls$percent_repub,na.rm=TRUE), max(Polls$percent_repub,na.rm=TRUE)), family = "Times")+  abline(lm(Polls$percent_repub ~ Polls$Pub_supp))+ segments(0,min(Polls$percent_repub), 1, max(Polls$percent_repub),col = "blue", lty = 2, lwd = par("lwd"))

plot(Polls$Pub_supp,Polls$prez_copartisans, pch = 16,  col = alpha("black", 0.5),main="% Congressional Copartisans", xlab="% Public Favoring Force", ylab="% Congressional Copartisans",cex.lab = 1.6,cex.main = 1.6,cex.sub=1.5,sub = paste( "R-Squared = ", round(performance$R2[9], digits = 2), sep = ""), xlim=c(0, 1), ylim=c(min(Polls$prez_copartisans,na.rm=TRUE), max(Polls$prez_copartisans,na.rm=TRUE)), family = "Times")+  abline(lm(Polls$prez_copartisans ~ Polls$Pub_supp))+ segments(0,min(Polls$prez_copartisans), 1, max(Polls$prez_copartisans),col = "blue", lty = 2, lwd = par("lwd"))

plot(Polls$Pub_supp,Polls$prez_copartisans, pch = 16,  col = alpha("black", 0.5),main="% Congressional Copartisans", xlab="% Public Favoring Force", ylab="% Congressional Copartisans",cex.lab = 1.6,cex.main = 1.6,cex.sub=1.5, sub = paste( "R-Squared = ", round(performance$R2[9], digits = 2), sep = ""), xlim=c(0, 1), ylim=c(min(Polls$prez_copartisans,na.rm=TRUE), max(Polls$prez_copartisans,na.rm=TRUE)), family = "Times")+  abline(lm(Polls$prez_copartisans ~ Polls$Pub_supp))+ segments(0,min(Polls$prez_copartisans), 1, max(Polls$prez_copartisans),col = "blue", lty = 2, lwd = par("lwd"))

dev.off()

rm(list = setdiff(ls(), c("CSUMF_speeches","CSUMF_CSS","CSUMF_Crises")))






#Figure A10: necessary condition analysis

library(NCA)

#We limit to initial uses of force (not crises part of ongoing war)
DF2 <- subset(CSUMF_CSS,CSUMF_CSS$continuing_war==0)

model <- nca_analysis(DF2,"avg_agg_support_Pre_Use_of_Force_ONLY_5adjust","US_high_act")
nca_output(model, plots = TRUE, summaries = FALSE)

# Default values copied from p_constants
line_colors <- list(ols="green",        lh="red3",            cols="darkgreen",
                    qr="blue",          ce_vrs="orchid4",     cr_vrs="violet",
                    ce_fdh="red",       cr_fdh="orange",      sfa="darkgoldenrod",
                    c_lp="blue")
line_types  <- list(ols=1,              lh=2,                 cols=3,
                    qr=4,               ce_vrs=5,             cr_vrs=1,
                    ce_fdh=6,           cr_fdh=1,             sfa=7,
                    c_lp=2)
line_width  <- 1.5
point_type  <- 21
point_color <- 'red'


line.colors['ce_fdh'] <- 'black'
line.colors['cr_fdh'] <- 'black'
line.colors['ols'] <- 'grey'
point.color <- "black"
point.type <- 20
line.width <- 2

display_plot <-
  function (plot) {
    flip.x <- plot$flip.x
    flip.y <- plot$flip.y
    
    # Determine the bounds of the plot based on the scope
    xlim <- c(plot$scope.theo[1 + flip.x], plot$scope.theo[2 - flip.x])
    ylim <- c(plot$scope.theo[3 + flip.y], plot$scope.theo[4 - flip.y])
    
    # Reset/append colors etc. if needed
    for (method in names(line_types)) {
      line.types[[method]] <- line_types[[method]]
    }
    if (is.numeric(line_width)) {
      line.width <- line_width
    }
    if (is.numeric(point_type)) {
      point.type <- point_type
    }
    if (point_color %in% colors()) {
      point.color <- point_color
    }
    # Only needed until the next release (3.0.2)
    if (!exists("point.color")) {
      point.color <- "blue"
    }
    
    # Plot the data points
    plot (plot$x, plot$y, pch=20, col="black", bg="black",
          xlim=xlim, ylim=ylim, xlab="\n \n Congressional Sentiment", ylab="Escalation Level \n \n \n", family = "Times", las = 1, yaxt = "n", xaxt = "n")
    axis(1, at = seq(-0.5, 0.5, by = 0.25),
         labels = c("-0.5 \n Uniform \n Opposition","-0.25 \n \n ","0.0 \n Neutral / Balanced \n ", "0.25 \n \n ","0.5 \n Uniform \n Support"), family = "Times", padj = 0.5, cex.axis=0.9, gap.axis = 0.1)
    
    axis(2, at = seq(1, 15),
         labels = c("No Resp.","Econ.","Threats","Show of Force","Advisers","Uncontested","0","1-5","6-25","26-50","51-100","101 to 250","251 to 500","501 to 1000","1000+"), las = 2, family = "Times")
    
    
    # Plot the scope outline
    abline(h=6.5, lty=2, col="grey")
    
    # Plot the legend before adding the clipping area
    legendParams = list()
    for (method in plot$methods) {
      line.color <- line.colors[[method]]
      line.type  <- line.types[[method]]
      
      name <- gsub("_", "-", toupper(method))
      legendParams$names  = append(legendParams$names, name)
      legendParams$types  = append(legendParams$types, line.type)
      legendParams$colors = append(legendParams$colors, line.color)
    }
    if (length(legendParams) > 0) {
      legend("topleft", cex=0.7, legendParams$names,
             lty=legendParams$types, col=legendParams$colors, bg=NA)
    }
    
    # Apply clipping to the lines
    clip(xlim[1], xlim[2], ylim[1], ylim[2])
    
    # Plot the lines
    for (method in plot$methods) {
      line <- plot$lines[[method]]
      line.color <- line.colors[[method]]
      line.type  <- line.types[[method]]
      
      if (method %in% c("lh", "ce_vrs", "ce_fdh")) {
        lines(line[[1]], line[[2]], type="l",
              lty=line.type, col=line.color, lwd=line.width)
      } else {
        abline(line, lty=line.type, col=line.color, lwd=line.width)
      }
    }
    
    # Plot the title
    title(paste0("Necessary Condition Analysis: Congressional Sentiment"), cex.main=1, family = "Times")
  }

pdf(file = "Output/Figure_A10_NCA.pdf",   # The directory you want to save the file in
    width = 10, # The width of the plot in inches
    height = 6)

par(mar=c(5,9,4,1))
display_plot(model$plots[[1]])
dev.off()

rm(list = setdiff(ls(), c("CSUMF_speeches","CSUMF_CSS","CSUMF_Crises")))






#Export Congressional Support Scores and Combine with Covariates for multivariate analysis

library(readr)
write_csv(CSUMF_CSS, "Output/CSUMF_CSS.csv")


Covariates <- read_csv("Data_files/Covariates.csv")
Multivariate_DF <- CSUMF_CSS[c("crisno","avg_agg_support_Pre_Use_of_Force_ONLY_5adjust","gpt_support_Pre_Use_of_Force_ONLY_5_adj","avg_agg_support_5adjust","gpt_support_5_adj")]
Multivariate_DF <- merge(Covariates,Multivariate_DF, by = "crisno")
library("writexl")
write_xlsx(Multivariate_DF,"Output/Multivariate_DF.xlsx")

rm(list = setdiff(ls(), c("CSUMF_speeches","CSUMF_CSS","CSUMF_Crises")))


